In [2]:
%pylab


Using matplotlib backend: agg
Populating the interactive namespace from numpy and matplotlib

In [3]:
%matplotlib inline

In [4]:
cd ..


/home/scott/Documents/git/neukrill-net-work

In [5]:
import sys
import numpy as np
import skimage
import cv2

In [6]:
import neukrill_net.utils as utils
import neukrill_net.image_processing as image_processing

In [7]:
from IPython.display import display
from IPython.display import Image
from IPython.display import HTML

In [8]:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

In [71]:
#img = skimage.io.imread('data/train/acantharia_protist/100224.jpg')
img = skimage.io.imread('data/train/acantharia_protist/64.jpg')

Had to set the edgeThreshold parameter manually because default of 31 removes the entire image with small images!


In [125]:
detector = cv2.ORB_create(nfeatures=500, edgeThreshold=0, patchSize=31)
norm = cv2.NORM_HAMMING

In [77]:
# One step process
kp, des = detector.detectAndCompute(img, None)

In [51]:
# Two step process

# find the keypoints with ORB
kp = detector.detect(img, None)

# compute the descriptors with ORB
kp, des = detector.compute(img, kp)

In [78]:
# draw only keypoints location,not size and orientation
#img2 = cv2.drawKeypoints(img, kp, color=(0,255,0), flags=0)
img2 = img
img2 = cv2.drawKeypoints(img, kp, img2, flags=0)
imgplot = plt.imshow(img2)
imgplot.set_cmap('gray')
plt.show()



In [79]:
len(kp)


Out[79]:
31

In [129]:
des[0:3,:]


Out[129]:
array([[128,   0,   4, 128,  44,   0, 136,   0, 136,   0,   0,  18, 140,
          2,  32,  67,  66,   1,   2,   0,  32,  68,   0, 192, 132,  16,
        138,   8,  20,   8,  33,   0],
       [  2,  14,   4, 128, 148, 227,  12,  79,  20, 166,  24, 147,  32,
         38,  33,  73,  65,  18, 131,  48,  98,  12,   1, 158,  40,   6,
        157, 197, 148,  72, 148,   8],
       [130, 141,   2, 135, 180, 137,  46, 167,  12, 229,  90, 135, 224,
         96,  50,  64, 208,  98,  51,  69, 112, 152,   8, 191,  44,  20,
        142, 192,  28,  94, 144,  72]], dtype=uint8)

In [127]:
type(des)


Out[127]:
numpy.ndarray

One of the code examples uses equalizeHist as a preprocessing step. Do we want to do this?


In [80]:
imgplot = plt.imshow(cv2.equalizeHist(img))
imgplot.set_cmap('gray')
plt.show()


Clearly not, as the abundance of white pixels has set everything else to be black! Might need to find a different way of normalising the images.

I want to check that we will get decent outputs for the largest and smallest of the images.


In [81]:
import neukrill_net.utils as utils
import neukrill_net.image_processing as image_processing

In [82]:
settings = utils.Settings('settings.json')

In [86]:
# Load the height and width of training data
X, names = utils.load_data(settings.image_fnames, processing=image_processing.attributes_wrapper(['width','height']), classes=settings.classes)

In [116]:
fullnames = [myname for classname in settings.classes for myname in settings.image_fnames['train'][classname]]

In [117]:
indexMinWidth  = numpy.argmin(X[:,0])
indexMinHeight = numpy.argmin(X[:,1])
indexMinPixels = numpy.argmin(np.prod(X,axis=1))
indexMaxWidth  = numpy.argmax(X[:,0])
indexMaxHeight = numpy.argmax(X[:,1])
indexMaxPixels = numpy.argmax(np.prod(X,axis=1))

In [118]:
indexList = [indexMinWidth, indexMinHeight, indexMinPixels, indexMaxWidth, indexMaxHeight, indexMaxPixels]

In [119]:
imgList = [skimage.io.imread(fullnames[index]) for index in indexList]

In [120]:
for img in imgList:
    print(img.shape)


(47, 31)
(21, 71)
(21, 41)
(122, 424)
(428, 303)
(399, 410)

In [126]:
for i,img in enumerate(imgList):
    imgplot = plt.imshow(img)
    imgplot.set_cmap('gray')
    plt.show()
    # Detect keypoints
    kp = detector.detect(img, None)
    print('Image {} has {} detected keypoints'.format(fullnames[indexList[i]],len(kp)))
    img2 = img
    img2 = cv2.drawKeypoints(img, kp, img2, flags=0)
    # Plot labelled figure
    imgplot = plt.imshow(img2)
    imgplot.set_cmap('gray')
    plt.show()


Image /home/scott/Documents/git/neukrill-net-work/data/train/acantharia_protist/101830.jpg has 30 detected keypoints
Image /home/scott/Documents/git/neukrill-net-work/data/train/chaetognath_other/124348.jpg has 16 detected keypoints
Image /home/scott/Documents/git/neukrill-net-work/data/train/trichodesmium_bowtie/48841.jpg has 21 detected keypoints
Image /home/scott/Documents/git/neukrill-net-work/data/train/tunicate_salp_chains/68261.jpg has 500 detected keypoints
Image /home/scott/Documents/git/neukrill-net-work/data/train/ctenophore_cestid/23299.jpg has 500 detected keypoints
Image /home/scott/Documents/git/neukrill-net-work/data/train/unknown_unclassified/73259.jpg has 500 detected keypoints

In [130]:
def describeImage(img):
    kp, des = detector.detectAndCompute(img, None)
    return des

In [132]:
X = np.vstack([describeImage(img) for img in imgList])

In [135]:
X.shape


Out[135]:
(1567, 32)

In [137]:



Out[137]:
9